package com.wf.atm.server;

import com.wf.atm.exception.ATMException;
import com.wf.atm.exception.BalanceNotEnoughException;
import com.wf.atm.exception.LoginException;
import com.wf.atm.exception.RegisterException;
import com.wf.atm.pojo.Account;
import com.wf.atm.service.Bank;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.LockSupport;

/**
 * @author mpk
 * @since 2024/6/17 20:13:04
 */
public class ServerTasker implements Runnable {

    private Socket socket;
    private Bank bank;
    private Thread thread;
    private CopyOnWriteArrayList<Socket> list;


    public ServerTasker(Socket socket, Bank bank, Thread thread, CopyOnWriteArrayList<Socket> list) {

        this.socket = socket;
        this.bank = bank;
        this.thread = thread;
        this.list = list;
        list.add(socket);

    }

    @Override
    public void run() {


        try {

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());


            String s;

            while ((s = bufferedReader.readLine()) != null) {

                String[] strings = s.split(",+");

                for (String string : strings) {
                    System.out.println(string);
                }
                Account account;
                switch (strings[0]) {
                    case "close":
                        try {
                            list.remove(socket);
                            socket.close();
                            System.out.println("客户端已下线~");
                            //  Object o = new Object();
                            //  oos.writeObject(o);
                            oos.flush();
                            LockSupport.unpark(thread);
                            return;

                        } catch (IOException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }

                    case "register":
                        try {
                            if(strings.length == 8) {
                                System.out.println("register");
                                account = bank.register(strings[1], strings[2], strings[3], strings[4], strings[5], strings[6], Integer.parseInt(strings[7]));
                                oos.writeObject(account);
                                oos.flush();
                            }
                            else
                            {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }
                            continue;
                        } catch (IOException | RegisterException|NumberFormatException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }

                    case "login":
                        try {
                           if(strings.length == 3) {
                               System.out.println("正在进行登录中...");
                               account = bank.login(Long.valueOf(strings[1]), strings[2]);
                               oos.flush();
                               oos.writeObject(account);
                           }else
                           {
                               oos.writeObject("参数错误");
                               oos.flush();
                           }
                        } catch (IOException | LoginException | NumberFormatException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }

                        continue;
                    case "deposit":
                        try {
                            if(strings.length == 3) {
                                System.out.println("正在进行deposit中...");
                                account = bank.deposit(Long.valueOf(strings[1]), Double.parseDouble(strings[2]));
                                oos.writeObject(account);
                                oos.flush();
                            }
                            else {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }

                        } catch (LoginException | ATMException | IOException | NumberFormatException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        continue;
                    case "withdraw":
                        try {
                            if(strings.length == 4) {
                                System.out.println("正在进行withdraw中...");
                                account = bank.withdraw(Long.valueOf(strings[1]), strings[2], Double.parseDouble(strings[3]));
                                oos.writeObject(account);
                                oos.flush();
                            }else
                            {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }

                        } catch (BalanceNotEnoughException | LoginException | IOException | NumberFormatException e) {
                            System.out.println(e.getMessage());
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        continue;
                    case "transfer":

                        try {
                            if(strings.length == 5) {
                                System.out.println("正在进行transfer中...");
                                bank.transfer(Long.valueOf(strings[1]), strings[2], Long.valueOf(strings[3]), Double.parseDouble(strings[4]));
                                account = bank.login(Long.valueOf(strings[1]), strings[2]);
                                oos.writeObject(account);
                                oos.flush();
                            }else
                            {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }

                        } catch (BalanceNotEnoughException | LoginException | ATMException | IOException |NumberFormatException e) {
                            System.out.println(e.getMessage());
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        continue;

                    case "updateCeiling":
                        try {
                            if(strings.length == 4) {
                                System.out.println("正在进行updateCeiling中...");
                                account = bank.updateCeiling(Long.valueOf(strings[1]), strings[2], Double.parseDouble(strings[3]));
                                oos.writeObject(account);
                                oos.flush();
                            }else
                            {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }

                        } catch (LoginException | ATMException|NumberFormatException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }

                        continue;
                    case "requestLoan":
                        try {
                            if(strings.length == 3) {
                                System.out.println("正在进行requestLoan中...");
                                account = bank.requestLoan(Long.valueOf(strings[1]), Double.parseDouble(strings[2]));
                                oos.writeObject(account);
                                oos.flush();
                            }else
                            {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }
                        } catch (BalanceNotEnoughException | LoginException|NumberFormatException | ATMException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }
                        continue;
                    case "payLoan":
                        try {
                            if(strings.length == 3) {
                                System.out.println("正在进行payLoan中...");
                                account = bank.payLoan(Long.valueOf(strings[1]), Double.parseDouble(strings[2]));
                                oos.writeObject(account);
                                oos.flush();
                            }
                            else
                            {
                                oos.writeObject("参数错误");
                                oos.flush();
                            }

                        } catch (LoginException | ATMException|NumberFormatException e) {
                            oos.writeObject(e.getMessage());
                            oos.flush();
                        }

                        continue;
                    default:
                        System.out.println("没有该指令");
                }

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


}
